<template>
	<view class="company-file">
		<bs-navbar title='企业档案'></bs-navbar>
		<bsSearch @confirm='confirm' ref='bsSearch' @handScreen='handScreen' @search='search'></bsSearch>
		<view class="file-box">
			<bsFileList :status='status' :list='companyRecordList' @handDetails='handDetails'></bsFileList>
		</view>
		<screenPopup @handReset='handReset' @handSubType='handSubType' ref='screenPopup'></screenPopup>
		<u-back-top :scroll-top="scrollTop"></u-back-top>
	</view>
</template>

<script>
	import bsSearch from '@/components/bs-search/bs-search.vue'
	import bsFileList from '@/components/bs-file-list/bs-file-list.vue'
	import screenPopup from './components/screenPopup.vue'
	export default {
		components: {
			bsSearch,
			bsFileList,
			screenPopup
		},
		data() {
			return {
				status: 'nomore',
				searchForm: {
					shoolName: '',
					recordName: '',
					type: 1,
					companyAddress: '',
				},
				pages: 0,
				companyRecordList: [],

				paging: {
					page: 1,
					pageSize: 10,
					type: 1,
				},
				scrollTop: 0
			};
		},

		onPageScroll(e) {
			this.scrollTop = e.scrollTop;
		},
		async onPullDownRefresh() {
			this.handReset(true)
			await this.init()
			uni.stopPullDownRefresh();
		},
		async onReachBottom() {
			await this.getMore()
		},
		async mounted() {
			await this.init()
		},
		methods: {
			//重置搜索
			async handReset(isToast) {
				this.$refs.screenPopup.typeList.forEach(item => {
					item.flg = false
				})
				this.$refs.screenPopup.provinceIndex = -1
				this.$refs.screenPopup.cityList = []
				this.$refs.screenPopup.cityIndex = -1
				for (let key in this.searchForm) {
					this.searchForm[key] = ''
				}
				this.searchForm.type = 1
				this.$refs.bsSearch.site = '全国'
				this.$refs.bsSearch.keyword = ''
				await this.init()
				if (!isToast) {
					uni.showToast({
						title: '重置成功',
						icon: 'none'
					})
				}

				this.$refs.screenPopup.hide()
			},
			//筛选
			async handSubType(shoolObj, companyAddress) {
				if (!shoolObj && !companyAddress) {
					uni.showToast({
						title: '请选择',
						icon: 'none'
					})
				} else {
					this.searchForm.companyAddress = companyAddress
					if (companyAddress) {
						this.$refs.bsSearch.site = companyAddress
					}
					if (shoolObj) {
						this.searchForm.shoolName = shoolObj.name
					}
					await this.init()
					this.$refs.screenPopup.hide()

				}
			},
			async getMore() {
				if (this.pages == this.paging.page) {
					this.status = 'nomore';
					return
				}
				this.status = 'loading';
				this.paging.page = ++this.paging.page;
				let companyRecordList = await this.$u.api.companyRecordList({
					...this.paging,
					...this.searchForm
				})
				this.pages = companyRecordList.pages
				this.companyRecordList = this.companyRecordList.concat(companyRecordList.list)
				this.status = 'loadmore';
			},
			async init() {
				this.paging.page = 1
				this.paging.type = 1
				this.status = 'loading';
				let companyRecordList = await this.$u.api.companyRecordList({
					...this.paging,
					...this.searchForm
				})

				this.pages = companyRecordList.pages
				this.companyRecordList = companyRecordList.list
				if (this.pages == this.paging.page) {
					this.status = 'nomore';
				}
				console.log(this.companyRecordList, '个人档案')

			},
			//级联城市选择
			async confirm(site) {
				this.searchForm.companyAddress = site
				// this.$refs.screenPopup.cityIndex = -1
				//修改级联同时修改弹框得删选选中状态
				let provinceIndex = this.$refs.screenPopup.provinceList.findIndex(item => item.value == site)
				console.log(provinceIndex)
				this.$refs.screenPopup.handProvince(this.$refs.screenPopup.provinceList[provinceIndex], provinceIndex)
				await this.init()
			},
			async search() {
				this.searchForm.recordName = this.$refs.bsSearch.keyword
				await this.init()
			},
			handScreen() {
				this.$refs.screenPopup.open()
			},
			handDetails(item) {
				uni.navigateTo({
					url: './companyFileDetails/companyFileDetails?recordId=' + item.recordId
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: $bs-bg-color-grey;
	}

	.company-file {
		background-color: $bs-bg-color-grey;
		height: 100%;

		.file-box {
			padding: 0 30rpx;
			padding-top: 40rpx;
		}
	}
</style>
